<?php
class EncounterRefraction extends ORDataObject {
	var $encounter_refraction_id			=	'';
	var $medical_record_id					=	'';
  	var $left_refraction_type				=	'';
  	var $right_refraction_type				=	'';
  	var $interocular_distance				=	'';
  	var $left_correct_glass					=	'';
  	var $right_correct_glass				=	'';
  	var $left_visual_acuity					=	'';
  	var $right_visual_acuity				=	'';
  	var $left_skiascopy						=	'';
  	var $right_skiascopy					=	'';
  	var $left_current_glass_diopter			=	'';
  	var $right_current_glass_diopter		=	'';
  	var $current_khoang_cach_tam_kinh		=	'';
  	var $left_glass_suggestion				=	'';
  	var $right_glass_suggestion				=	'';
  	var $doctor_id							=	'';
	var $doctor_updated_id         			=   '';
	var $encounter_date             		=   '';
	var $last_updated               		=   '';
	var $note								=	'';
	
	var $_table				=	'encounter_refraction';
	var $_key				=	'encounter_refraction_id';
	var	$_foreignKeyList	=	array(
		'medical_record_id'		=>	'medical_record'
	);
	
	public function EncounterRefraction(){
		parent::ORDataObject();
	}
	
	public function setup($id = 0){
		$this->set('id',$id);
		if($id>0){
			$this->populate();
		}
	}
	
	public function populate(){
		parent::populate('encounter_refraction_id');
	}
	
	public function setData($record_id, $left_refraction_type, $right_refraction_type
		, $interocular_distance, $left_correct_glass, $right_correct_glass, $left_visual_acuity
		, $right_visual_acuity, $left_skiascopy, $right_skiascopy, $left_current_diop
		, $right_current_diop, $current_KCTK, $left_glass_suggestion, $right_glass_suggestion
		, $doctor_id, $doctor_updated_id, $note){
			$data	=	array(
				'medical_record_id'						=>	$record_id,
				'left_refraction_type'					=>	$left_refraction_type,
  				'right_refraction_type'					=>	$right_refraction_type,
				'interocular_distance'					=>	$interocular_distance,
				'left_correct_glass'					=>	$left_correct_glass,
				'right_correct_glass'					=>	$right_correct_glass,
				'left_visual_acuity'					=>	$left_visual_acuity,
				'right_visual_acuity'					=>	$right_visual_acuity,
				'left_skiascopy'						=>	$left_skiascopy,
				'right_skiascopy'						=>	$right_skiascopy,
				'left_current_glass_diopter'			=>	$left_current_diop,
				'right_current_glass_diopter'			=>	$right_current_diop,
				'current_khoang_cach_tam_kinh'			=>	$current_KCTK,
				'left_glass_suggestion'					=>	$left_glass_suggestion,
				'right_glass_suggestion'				=>	$right_glass_suggestion,
				'doctor_id'								=>	$doctor_id,
				'doctor_updated_id'         			=>  $doctor_updated_id,
				'encounter_date'						=>  $this->getDbCreateDate(),
				'note'									=> 	$note
			);
			$this->populateArray($data);
	}
	
	public function setId($id){
		$this->encounter_refraction_id	=	$id;
	}
	
	public function getId(){
		return $this->encounter_refraction_id;
	}
	
	public function getRecordId(){
		return $this->medical_record_id;
	}
	
	public function getLeftRefrationType() {
		return $this->left_refraction_type;
	}

	public function getRightRefrationType() {
		return $this->right_refraction_type;
	}	

	public function getLeftCorrectGlass(){
		return $this->left_correct_glass;
	}
	
	public function getRightCorrectGlass(){
		return $this->right_correct_glass;
	}
	
	public function getLeftVisualAcuity(){
		return $this->left_visual_acuity;
	}
	
	public function getRightVisualAcuity(){
		return $this->right_visual_acuity;
	}
	
	public function getLeftSkiascopy(){
		return $this->left_skiascopy;
	}
	
	public function getRightSkiascopy(){
		return $this->right_skiascopy;
	}
	
	public function getLeftCurrentDiopter(){
		return $this->left_current_glass_diopter;
	}
	
	public function getRightCurrentDiopter(){
		return $this->right_current_glass_diopter;
	}
	
	public function getCurrentKCTK(){
		return $this->current_khoang_cach_tam_kinh;
	}
	
	public function getLeftSuggestion(){
		return $this->left_glass_suggestion;
	}
	
	public function getRightSuggestion(){
		return $this->right_glass_suggestion;
	}
	
	public function getInterocularDistance(){
		return $this->interocular_distance;
	}
	
	public function getNote() {
		return $this->note;
	}
	
	public function getObjectFromRecordId($recordId) {
		$sql = "SELECT $this->_key FROM $this->_table WHERE medical_record_id = $recordId";
		$res = $this->_execute($sql);
		
		$obj = ORDataObject::factory('EncounterRefraction');
		if (!$res->EOF) {
			$obj->setup($res->fields[$this->_key]);
		}
		return $obj;
	}
	
	public function getDoctorId(){
		return $this->doctor_id;
	}
	
	public function getDoctorName(){
		$person	= ORDataObject::factory('Person');
		$person->setup($this->doctor_id);
		return $person->getName();
	}
	
	public function getUpdatedDoctorName(){
		$person	= ORDataObject::factory('Person');
		$person->setup($this->doctor_updated_id);
		return $person->getName();
	}
	
	public function getDate($whichDate, $format='%d/%m/%Y') {
		if ($whichDate == 'created') {
			$sql = "SELECT encounter_date from $this->_table WHERE encounter_refraction_id = $this->encounter_refraction_id";
			$res = $this->_execute($sql);
			$date = $res->fields['encounter_date'];
		} else {
			$sql = "SELECT last_updated from $this->_table WHERE encounter_refraction_id = $this->encounter_refraction_id";
			$res = $this->_execute($sql);
			$date = $res->fields['last_updated'];
		}
		
		$arr = explode(" ", $date);
		$date = DateObject::createFromISO($arr[0]);
		return $date->toString($format) . " " . $arr[1];		 
	}
	
	public function getDbCreateDate(){
		$sql = "SELECT encounter_date from $this->_table WHERE encounter_refraction_id = $this->encounter_refraction_id";
		$res = $this->_execute($sql);
		if (!empty($res)) {
			return $res->fields['encounter_date'];
		} else {
			return "1970-01-01 01:01:01";
		}
	}
	
	function persist($encounter_date='', $last_updated='') {
		$res = parent::persist();
		if ($res == false) {
			return $res;
		}
		$sql = "UPDATE $this->_table SET last_updated = NOW() WHERE encounter_refraction_id = $this->encounter_refraction_id";
		$this->_execute($sql);
		if ($encounter_date == '') {
			$sql = "UPDATE $this->_table SET encounter_date = NOW() WHERE encounter_refraction_id = $this->encounter_refraction_id";
			$this->_execute($sql);
		}
		return $res;
	}
	
	public function getRefractionIdFromTicketItem($itemId) {
		$sql = "SELECT encounter_refraction_id FROM $this->_table er
		        LEFT JOIN medical_record mr ON er.medical_record_id = mr.medical_record_id
		        LEFT JOIN ticket_item ti ON mr.ticket_id = ti.ticket_id
		        WHERE ti.ticket_item_id = $itemId";
		$res = $this->_execute($sql);
		if (empty($res)) {
			return 0;
		}
		return $res->fields['encounter_refraction_id'];
	}
	
	public function updateDoctorId($doctorId, $refId) {
		$this->_execute("UPDATE $this->_table SET doctor_id = $doctorId WHERE encounter_refraction_id = $refId");
	}
	
	public function getLatestRefraction($patientId) {
		$sql = "SELECT MAX(er.medical_record_id) AS medical_record_id
				FROM $this->_table er LEFT JOIN medical_record mr ON (er.medical_record_id = mr.medical_record_id)
				WHERE mr.patient_id = $patientId";
		$res = $this->_execute($sql);
		if (empty($res) || $res->fields['medical_record_id'] == '') {
			return 0;
		}
		return $res->fields['medical_record_id'];
	}
}
?>